package com.sleepcamel.exporter.test


import junit.framework.TestCase

import com.sleepcamel.exporter.DBExporter;
import com.sleepcamel.exporter.model.category.StringCategory
import com.sleepcamel.exporter.model.generator.JPACodeGenerator
import com.sleepcamel.exporter.model.generator.JavaModelGenerator
import com.sleepcamel.exporter.persister.XMLPersister
import com.sleepcamel.exporter.reader.DataBaseMetaDataReader
import com.sleepcamel.exporter.util.JdbcUtils
import com.sleepcamel.rest.domain.Mapping;
import com.sleepcamel.rest.domain.Project;
import com.sleepcamel.rest.domain.ProjectPackage;
import com.sleepcamel.rest.domain.method.RESTMethod;
import com.sun.org.apache.xpath.internal.functions.Function2Args.Arg1Owner;


class DatabaseReverseXMLTest extends TestCase{

	static JdbcUtils jdbcUtils = new JdbcUtils(url:"jdbc:h2:file:/db-codeGen/testdb", userName:"sa", password:"", driver:"org.h2.Driver")
	XMLPersister persister = new XMLPersister()
	File dbDir

	static String sqlDDL = '''
			DROP TABLE IF EXISTS EMPLOYEE, DEPARTMENT, ROLE, ROLE_EMPLOYEE;
			
			CREATE TABLE DEPARTMENT (
			  ID INTEGER AUTO_INCREMENT PRIMARY KEY,
			  name VARCHAR(30)
			);
			
			CREATE TABLE ROLE (
			    ROLE_ID INTEGER AUTO_INCREMENT PRIMARY KEY,
			    NAME VARCHAR(30)
			);
			
			CREATE TABLE EMPLOYEE (
			  EMP_ID INTEGER AUTO_INCREMENT PRIMARY KEY,
			  FK_DEPARTMENT_ID INTEGER NULL,
			  firstName VARCHAR(30),
			  LAST_NAME VARCHAR(30),
			  notNullVariable VARCHAR(30) not null,
			  matchingname VARCHAR(255),
			  FOREIGN KEY (FK_DEPARTMENT_ID) REFERENCES DEPARTMENT(ID)
			);
			
			
			CREATE TABLE ROLE_EMPLOYEE (
			    FK_ROLE_ID INTEGER,
			    FK_EMP_ID INTEGER,
			    
			    FOREIGN KEY (FK_ROLE_ID) REFERENCES ROLE(ROLE_ID),
			    FOREIGN KEY (FK_EMP_ID) REFERENCES EMPLOYEE(EMP_ID)
			);
			''';

	
	public void tearDown() {
		//dbDir.deleteDir();
	}
	
	public void setUp () {
		dbDir = new File("./db-codeGen")
		
		if(!dbDir.exists()){
			dbDir.mkdir()
		}
		
		dbDir.eachFile {File file ->
			file.delete()
		}
				
		jdbcUtils.executeScript(sqlDDL)
		persister.outputDir = dbDir
	}
	
	public void testExporter() {
		DBExporter exporter = new DBExporter()

		use(StringCategory) {
			exporter.jdbcUtils = jdbcUtils
			exporter.modelDirectory = dbDir
			exporter.generateModelFiles();

			assertNotNull(new File(persister.outputDir, "codeGen.xml").text)
		}
	}
	
}